package com.hb.trade.server.center.au.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.center.au.dto.OrgPayeeDTO;
import com.hb.core.pojo.center.au.entity.OrgPayee;
import com.hb.core.pojo.center.au.mapper.OrgPayeeMapper;
import com.hb.trade.server.center.au.service.OrgPayeeService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 企业收款方表(子表au_org_collection) 服务实现类
 * </p>
 *
 * @author zhongdl
 * @since 2022-02-14
 */
@Service
public class OrgPayeeServiceImpl extends ServiceImpl<OrgPayeeMapper, OrgPayee> implements OrgPayeeService {

    /**
     *
     * @Description: 查询收款方列表
     * @Author: zhongdl
     * @Date:2022/2/14 10:40
     * @param orgId:
     * @return: {@link List< OrgPayeeDTO> }
     **/
    @Override
    public List<OrgPayeeDTO> listOrgPayee(Long orgId) {
        List<OrgPayeeDTO> list = CollUtil.list(false);
        QueryWrapper<OrgPayee> wrapper = OrgPayeeDTO.wrapper("id","payee","org_id");
        wrapper.eq("org_id",orgId);
        List<OrgPayee> orgPayees = list(wrapper);
        if (CollUtil.isEmpty(orgPayees)){
            return list;
        }
        for (OrgPayee orgPayee : orgPayees) {
            list.add(orgPayee.to(OrgPayeeDTO.class));
        }
        return list;
    }

    /**
     *
     * @Description: 查询收款方列表(分页)
     * @Author: zhongdl
     * @Date:2022/2/14 10:40
     * @param orgPayeeDTO:
     * @return: {@link List< OrgPayeeDTO> }
     **/
    @Override
    public Page<OrgPayeeDTO> pageOrgPayee(OrgPayeeDTO orgPayeeDTO) {
        Page<OrgPayee> pages = orgPayeeDTO.pages();
        QueryWrapper<OrgPayee> wrapper = OrgPayeeDTO.wrapper("id","payee","org_id");
        wrapper.eq("org_id",orgPayeeDTO.getOrgId());
        if (StrUtil.isNotEmpty(orgPayeeDTO.getPayee())) {
            wrapper.like("payee", orgPayeeDTO.getPayee());
        }
        Page payeePage = this.baseMapper.selectPage(pages, wrapper);
        List<OrgPayee> records = payeePage.getRecords();
        if (CollUtil.isEmpty(records)){
            return payeePage;
        }
        List<OrgPayeeDTO> list = records.stream().map(item -> {
            OrgPayeeDTO dto = item.to(OrgPayeeDTO.class);
            return dto;
        }).collect(Collectors.toList());
        payeePage.setRecords(list);
        return payeePage;
    }
}
